package
{
	import com.candykingdom.base.Display;
	import com.candykingdom.components.Image;
	import com.candykingdom.components.Scroll;
	import com.candykingdom.events.ControlEvent;
	import com.candykingdom.system.DeviceInfo;
	import com.candykingdom.system.Systems;
	import com.candykingdom.utils.Config;
	import com.candykingdom.utils.FPS;
	
	import flash.desktop.NativeApplication;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	import flash.utils.setTimeout;
	
	[SWF(width="640", height="1136", frameRate="30", backgroundColor="#CDE9EB")]
	public class CandyKingdomAPP extends Display
	{
		[Embed(source="assets/test.jpg")]
		private var background_class:Class;
		private var _background:Bitmap
		
		public function CandyKingdomAPP()
		{
			super();
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.quality = StageQuality.HIGH;
			//scrollRect = new Rectangle(0 ,0 ,640 ,1136);
			
			_background = new background_class();
			background_class = null;
			_background.smoothing = true;
			_background.width = stage.fullScreenWidth/640*_background.width;
			_background.height = stage.fullScreenHeight/1136*_background.height;
			_background.x = (stage.fullScreenWidth-_background.width)/2;
			_background.y = (stage.fullScreenHeight-_background.height)/2;
			parent.addChild(_background);
			
			loaderInfo.addEventListener(Event.COMPLETE 
				,function(event:Event):void{stage?init(null):addEventListener(Event.ADDED_TO_STAGE ,init);});
		}
		
		private function init(event:Event):void
		{
			/** 自适应 */
			Config.autoScale(this ,640 ,1136 ,35);
			
			/** 节约内存 */
			NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, function (e:Event):void {visible = true;});
			NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, function (e:Event):void {visible = false;});
			
			/** 操控模式 */
			if(DeviceInfo.platform=="Windows")
			{
				Multitouch.inputMode = MultitouchInputMode.NONE;
			}else{
				Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
			}
			
			/** 删除启动画面 */
			if(_background!=null)setTimeout(parent.removeChild ,1000 ,_background);
			
			/** FPS RAM */
			FPS.Start(stage);
			
			/** 首次回收 */
			Systems.gc();
			
			/** 启用拖动，并监听事件 */
			this.enabledDrag = true;
			this.addEventListener(ControlEvent.DRAG_START ,dragHandler);
			this.addEventListener(ControlEvent.DRAG_STOP ,dragHandler);
			this.addEventListener(ControlEvent.DRAG_MOVE ,dragHandler);
			this.addEventListener(ControlEvent.DRAG_CAST ,dragHandler);
			
			
			//var ix:PageIndex = new PageIndex();
			//addChild(ix);
			
			var me:PageMenus = new PageMenus();
			addChild(me);
			
			/*var sc:Scroll = new Scroll();
			sc.addChild(new Sprite());
			sc.addChild(me);
			addChild(sc);*/
			
			this.viewRect = new Rectangle(0 ,0 ,640 ,1136);
		}
		
		
		
		private var _moveX:Number=0;
		private var _moveY:Number=0;
		private var _d:int;
		private function dragHandler(event:ControlEvent):void
		{
			switch(event.type)
			{
				case ControlEvent.DRAG_START:
					this.draw(false);
					return;
					
				case ControlEvent.DRAG_MOVE:
					this.move(event.endPoint.x - event.startPoint.x + _moveX ,event.endPoint.y - event.startPoint.y + _moveY);
					//this.move(event.endPoint.x - event.startPoint.x ,event.endPoint.y - event.startPoint.y);
					return;
					
				case ControlEvent.DRAG_STOP:
					_moveX = this.moveX;
					_moveY = this.moveY;
					//this.clear();
					return;
					
				case ControlEvent.DRAG_CAST:
					trace(event.direction);
					return;
			}
		}
	}
}